package org.inspursc.s0517.health.evaluation.controller;


import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.enums.UserTypeEnum;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.util.SpringSecurityUtils;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.dto.HealthResultSectionCountDTO;
import org.inspursc.s0517.health.evaluation.dto.OrganizationCountDTO;
import org.inspursc.s0517.health.evaluation.dto.TwoLevelCategoryDTO;
import org.inspursc.s0517.health.evaluation.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yuchu
 */
@RestController
@RequestMapping("statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 机构统计
     * @return
     */
    @GetMapping("/organizationCount")
    public ResponseEntity<ResponseVO> organizationCount(HttpServletRequest request){
        List<OrganizationCountDTO> dto;
        SysUser user = SpringSecurityUtils.getCurrentUser(request);
        if(user.getUserType().equals(UserTypeEnum.ADMIN.getCode())) {
            dto = statisticsService.getOrganizationCount();
        }
        else {
            Integer orgId = user.getOrganizationId();
            dto = statisticsService.getOneOrganizationCount(orgId);
        }
        return ResponseUtil.success(dto);
    }

    @GetMapping("/heathResultCount")
    public ResponseEntity<ResponseVO> heathResultCount(HttpServletRequest request){
        List<HealthResultSectionCountDTO> listCount;
        SysUser user = SpringSecurityUtils.getCurrentUser(request);
        if(user.getUserType().equals(UserTypeEnum.ADMIN.getCode())) {
            listCount = statisticsService.getHeathResultCount();
        }
        else {
            Integer orgId = user.getOrganizationId();
            listCount = statisticsService.getHeathResultCountByOrg(orgId);
        }
        return ResponseUtil.success(listCount);
    }

    @GetMapping("/twoLevelCategoryAvg")
    public ResponseEntity<ResponseVO> twoLevelCategoryAvg(HttpServletRequest request){
        List<TwoLevelCategoryDTO> dtos;
        SysUser user = SpringSecurityUtils.getCurrentUser(request);
        if(user.getUserType().equals(UserTypeEnum.ADMIN.getCode())) {
            dtos = statisticsService.getTwoLevelCategoryAvg();
        }
        else {
            Integer orgId = user.getOrganizationId();
            dtos = statisticsService.getTwoLevelCategoryAvgByOrg(orgId);
        }
        return ResponseUtil.success(dtos);
    }
}
